342A - Xenia and Divisors - CodeForces Solution


greedy implementation *1200

Please click on ads to support us..

Python Code:

from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
ans = []
c = Counter(arr)
for i in range((n//3) + 1):
    if c[4] > 0 and c[2] > 0 and c[1] > 0:
        ans.append(['1', '2', '4'])
        c[4] -= 1
        c[2] -= 1
        c[1] -= 1
    elif c[6] > 0 and c[2] > 0 and c[1] > 0:
        ans.append(['1', '2', '6'])
        c[6] -= 1
        c[2] -= 1
        c[1] -= 1
    elif c[6] > 0 and c[3] > 0 and c[1] > 0:
        ans.append(['1', '3', '6'])
        c[6] -= 1
        c[3] -= 1
        c[1] -= 1
if any(x > 0 for x in c.values()):
    print(-1)
else:
    for group in ans:
        print(' '.join(group))

C++ Code:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    int x[n];
    int a=0,b=0,c=0,d=0,e=0;
    for(int i=0;i<n;i++){
        cin>>x[i];
        if(x[i]==1){
            a++;
        }
        else if(x[i]==2){
            b++;
        }
        else if(x[i]==3){
            c++;
        }
        else if(x[i]==4){
            d++;
        }
        else if(x[i]==6){
            e++;
        }
    }
    if((a==n/3) && (b+c==n/3) && (d+e==n/3)){
        if((b>=d) && (e>=c)){
            for(int i=0;i<d;i++){
                cout<<1<<" "<<2<<" "<<4<<endl;
            }
            b=b-d;
            for(int i=0;i<b;i++){
                cout<<1<<" "<<2<<" "<<6<<endl;
            }
            e=e-b;
            for(int i=0;i<e;i++){
                cout<<1<<" "<<3<<" "<<6<<endl;
            }
        }
        else{
            cout<<-1<<endl;
        }
    }
    else{
        cout<<-1;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password
1646B - Quality vs Quantity
80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera